package cn.bugstack.domain.activity.service;

// import cn.bugstack.domain.activity.adapter.repository.IActivityRepository;
import cn.bugstack.domain.activity.adapter.repository.IActivityRepository;
import cn.bugstack.domain.activity.model.entity.MarketProductEntity;
import cn.bugstack.domain.activity.model.entity.TrialBalanceEntity;
// import cn.bugstack.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
// import cn.bugstack.domain.activity.model.valobj.TeamStatisticVO;
import cn.bugstack.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import cn.bugstack.domain.activity.model.valobj.TeamStatisticVO;
import cn.bugstack.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.bugstack.wrench.design.framework.tree.StrategyHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 首页营销服务
 */
@Service
public class IndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService {

    // 注入策略工厂，在 spring 注册时，就已经将根节点定义
    @Resource
    private DefaultActivityStrategyFactory defaultActivityStrategyFactory;
    @Resource
    private IActivityRepository repository;

    // 首页试算执行：1、Root：前置参数判断。2、Switch：降级，切量拦截。3、异步数据加载，逻辑判断，调用折扣服务进行试算
    // 4、Tag：根据用户标签，判断折扣是否可用，5、End：封装折扣试算的实体对象
    @Override
    public TrialBalanceEntity indexMarketTrial(MarketProductEntity marketProductEntity) throws Exception {
        // 获取执行策略，获得流程起点
        StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity>
                strategyHandler = defaultActivityStrategyFactory.strategyHandler();
        // 受理试算操作
        return strategyHandler.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
    }

    // 查询 该用户在该活动下的前 ownerCount个拼团 和 非属该用户在该活动的 随机 randomCount个拼团
    @Override
    public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId
            , String userId, Integer ownerCount, Integer randomCount) {
        // 所有的拼团
        List<UserGroupBuyOrderDetailEntity> unionAllList = new ArrayList<>();

        // 查询用户在某活动下 前n个 未完成的拼团明细
        if (0 != ownerCount) {
            List<UserGroupBuyOrderDetailEntity> ownerList =
                 repository.queryInProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
            if (null != ownerList && !ownerList.isEmpty()){
                unionAllList.addAll(ownerList);
            }
        }

        // 查询非属用户 随机的 未完成的拼团明细
        if (0 != randomCount) {
            List<UserGroupBuyOrderDetailEntity> randomList =
                 repository.queryInProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, randomCount);
            if (null != randomList && !randomList.isEmpty()){
                unionAllList.addAll(randomList);
            }
        }

        return unionAllList;
    }

    // 统计活动拼团的数据   队伍总数, 成团数量, 参与总人数
    @Override
    public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
        return repository.queryTeamStatisticByActivityId(activityId);
    }

}
